<?php
/**
 * Created by PhpStorm.
 * User: zhangxinke
 * Date: 2019/6/1
 * Time: 8:50
 */

namespace app\api\model;


class AddressModel extends BaseModel
{
    protected $table = "xst_area";//数据表

//申请开店店铺类型下拉菜单显示
    public function address(){
        return db('xst_shop_type')->select();
    }
	//申请开店添加
    public function addressadd($uid,$name,$address,$type){
        return Db::name('shop')->insert([
            'uid'=>$uid,
            'name'=>$name,
            'type'=>'4',
            'address'=>'山东',
			'status'=>0,
			'createtime'=>time()
        ]);
    }
	//收货地址删除
    public function areadel($id){
        return $this->where('id',$id)->delete();
    }
	public function oneid($id){
        return $this->where('id',$id)->count();
    }
	//收货地址添加
    public function areaadd($id,$name,$phone,$area,$address,$flg){
      if($flg==1){
			$res=$this->where('uid',$id)->where('flg',1)->update(['flg'=>0]);
		}
        return $this
             ->insert([
			'uid'=>$id,
            'phone'=>$phone,
            'name'=>$name,
            'area'=>$area,
			'flg'=>$flg,
            'address'=>$address,
        ]);
    }
	//收货地址修改
    public function areaupdate($id,$uid,$name,$phone,$area,$address){
        return $this
             ->where('id',$id)->update([
			'uid'=>$uid,
            'phone'=>$phone,
            'name'=>$name,
            'area'=>$area,
			'flg'=>0,
            'address'=>$address,
        ]);
    }
	//收货地址显示
	public function areaindex($uid){
        return $this->where('uid',$uid)->select();
    }
	//默认收货地址修改
    public function areaupdates($id,$uid){
		$count=$this->where('uid',$uid)->where('flg',1)->count();
		if($count>0){
			$res=$this->where('uid',$uid)->where('flg',1)->update(['flg'=>0]);
			if($res){
				 return $this
				->where('id',$id)
				->update(['flg'=>1]);
			}else{
				return 0;
			}
		}else{
			 return $this
            ->where('id',$id)
            ->update(['flg'=>1]);
		}
       
    }
}